package Messages;

import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import Server.SQLConnection;
import ocsf.server.ConnectionToClient;

public class MessageRun {
	
	private ConnectionToClient client ;
	private ArrayList<Object> str ; 
	public static String TableName;
	private ResultSet RunNewRs;
	static int row1;

	
public MessageRun(ArrayList<Object> msg, ConnectionToClient client) {
		
		this.client= client ;
		this.str= msg; 		
		
	}

public void Run(String ScenarioId, String Version, String Owner) throws SQLException, IOException {
	
	SQLConnection sc = new SQLConnection();
	
	java.sql.PreparedStatement ScenarioName = sc.getConnection().prepareStatement("SELECT * FROM hts.SBank b where idSBank='"+ScenarioId+"';");
	ResultSet RunRs= ScenarioName.executeQuery();
	
	if (!RunRs.next())
	{		
		client.sendToClient("Error"); // if the user was not found 
		
	}
	else {
		 TableName=new StringBuilder().append(RunRs.getString(2)).append("_").append(Version).toString();
		 System.out.println(TableName);
	
		 PreparedStatement ScenarioNew = sc.getConnection().prepareStatement("create table "+TableName+" as(SELECT * FROM hts."+RunRs.getString(2)+") ;");
		 PreparedStatement ScenarioKey = sc.getConnection().prepareStatement("ALTER TABLE "+TableName +" ADD PRIMARY KEY (idScenario);");

		 		
		ScenarioNew.executeUpdate();
		ScenarioKey.executeUpdate();
		
		 
		 
		}
	java.sql.PreparedStatement ScenarioTemp = sc.getConnection().prepareStatement("SELECT t.`idScenario`,t.`TestDescription`,t.`Status` FROM hts."+TableName+" t;");


	RunNewRs= ScenarioTemp.executeQuery();
	
	ArrayList<Object> Run =new ArrayList<Object>();
	Run.add("RunStepFirst");
	Run.add(TableName);
	Run.add("1");
	while (RunNewRs.next())
	{
		Run.add(RunNewRs.getInt(1));
		Run.add(RunNewRs.getString(2));
		Run.add(RunNewRs.getString(3));
	}
	
	
	PreparedStatement newps = sc.getConnection().prepareStatement("INSERT INTO Run (idRun,SName,Status,per,Owner) VALUES(?,?,?,?,?);");
	PreparedStatement Allps = sc.getConnection().prepareStatement("SELECT * FROM Run;");
	
	ResultSet Allrs= Allps.executeQuery();
	row1 = 0;
	while(Allrs.next()){
		int temp = Allrs.getInt(1);
			if(row1>temp)
				row1=row1;
				else{
				row1=temp;
				}
	
	}

	newps.setInt(1, this.row1+1);
	newps.setString(2,TableName);
	newps.setString(3,"Run");
	newps.setInt(4, 0);
	newps.setString(5,Owner);
	newps.executeUpdate();
	
	
	
	PreparedStatement projectps = sc.getConnection().prepareStatement("INSERT INTO project (idProject,SName,Project) VALUES(?,?,?);");
	PreparedStatement Allprojectps = sc.getConnection().prepareStatement("SELECT * FROM project;");
	
	ResultSet AllProjectrs= Allprojectps.executeQuery();
	row1 = 0;
	while(AllProjectrs.next()){
		int temp = AllProjectrs.getInt(1);
			if(row1>temp)
				row1=row1;
				else{
				row1=temp;
				}
	
	}

	projectps.setInt(1, this.row1+1);
	projectps.setString(2,TableName);
	projectps.setString(3,RunRs.getString(4));
	projectps.executeUpdate();

	
	newps.close();
	RunRs.close();
	ScenarioTemp.close();
	ScenarioName.close();
	client.sendToClient(Run);
	

	}//end of Run
		
	

}//End of class
